package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.reggie.bean.Employee;
import com.example.reggie.service.EmployeeService;
import com.example.reggie.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest httpServletRequest, @RequestBody Employee employee){
        String username=employee.getUsername();
        LambdaQueryWrapper<Employee> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Employee::getUsername,username);
        Employee employee2=employeeService.getOne(lambdaQueryWrapper);
        if (employee2==null){
            return R.error("用户名或密码错误。");
        }
        if (employee2.getStatus()!=1){
            return R.error("账户处于锁定状态，无法登陆。");
        }
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
//        log.info(password);
//        log.info(employee.getPassword());
        if (!employee2.getPassword().equals(password)){
            return R.error("用户名或密码错误。");
        }

        httpServletRequest.getSession().setAttribute("employee",employee2.getId());
        return R.success(employee2);
    }

    @RequestMapping("/logout")
    public R<String> logout(HttpServletRequest httpServletRequest){
        httpServletRequest.getSession().removeAttribute("employee");
        return R.success("退出登录成功。");
    }

    @PostMapping
    public R<String> add(@RequestBody Employee employee){
        employeeService.save(employee);
        return R.success("添加用户成功");
    }

    @GetMapping("/page")
    public R<Page> list(long page,long pageSize,String name){
        LambdaQueryWrapper<Employee> wrapper=new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(name),Employee::getName,name);
        Page pageObj=new Page(page,pageSize);
        employeeService.page(pageObj,wrapper);
        return R.success(pageObj);
    }

    @PutMapping
    public R<String> update(@RequestBody Employee employee){
        employeeService.updateById(employee);
        return R.success("修改状态成功");
    }

    @GetMapping("/{id}")
    public R<Employee> getOne(@PathVariable Long id){
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getId,id);
        Employee employee = employeeService.getOne(queryWrapper);
        return R.success(employee);
    }
}
